﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using System.Linq;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmDmDichVuChiTiet : DevExpress.XtraEditors.XtraForm
    {
        DmDichVuBusiness dmDichVuBusiness = new DmDichVuBusiness();
        IList<DmDichVuEntity> lstDmDichVuEntity = null;

        DmDichVuChiTietBusiness dmDichVuChiTietBusiness = new DmDichVuChiTietBusiness();
        IList<DmDichVuChiTietEntity> lstDmDichVuChiTietEntity = null;

        GridCheckMarksSelection gridCheckMarksSelection = null;

        public frmDmDichVuChiTiet()
        {
            InitializeComponent();

            gridCheckMarksSelection = new GridCheckMarksSelection(gvDichVuChiTiet);
            LoadDichVu();
        }

        void LoadDichVu()
        {
            lstDmDichVuEntity = dmDichVuBusiness.GetDmDichVuList();
            var dmDichVuEntity = new DmDichVuEntity()
            {
                MaDichVu = ConstantManagerClinic.sValueMember,
                TenDichVu = ConstantManagerClinic.sDisplayMember
            };
            lstDmDichVuEntity.Insert(0, dmDichVuEntity);

            lkDichVu.Properties.DataSource = lstDmDichVuEntity;
            lkDichVu.Properties.DisplayMember = "TenDichVu";
            lkDichVu.Properties.ValueMember = "MaDichVu";

            lkDichVu.EditValue = ConstantManagerClinic.sValueMember;

        }

        void LoadDataGrid(object sender, EventArgs e)
        {
            lstDmDichVuChiTietEntity = dmDichVuChiTietBusiness.GetDmDichVuChiTietList();
            if (lkDichVu.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstDmDichVuChiTietEntity = lstDmDichVuChiTietEntity.Where(n => n.MaDichVu == lkDichVu.EditValue.ToString()).ToList();
            }

            grdDichVuChiTiet.DataSource = lstDmDichVuChiTietEntity;
        }

        private void frmDmDichVuChiTiet_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadDataGrid(sender, e);
        }

        private void lkDichVu_EditValueChanged(object sender, EventArgs e)
        {
            LoadDataGrid(sender, e);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmDichVuChiTiet((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        DmDichVuChiTietEntity getDataDmDichVuEntity()
        {
            if (gvDichVuChiTiet.RowCount > 0)
            {
                return (DmDichVuChiTietEntity)gvDichVuChiTiet.GetFocusedRow();
            }
            return null;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var dmDichVuChiTietEntity = getDataDmDichVuEntity();
            if (dmDichVuChiTietEntity == null) return;

            var frm = new frmCapNhatDmDichVuChiTiet((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmDichVuChiTietEntity);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void grdDichVuChiTiet_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;

            var dmDichVuChiTietEntity = getDataDmDichVuEntity();
            if (dmDichVuChiTietEntity == null) return;

            var frm = new frmCapNhatDmDichVuChiTiet((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmDichVuChiTietEntity);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        private void gvDichVuChiTiet_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmDichVuChiTietEntity dm in deleteObjects)
            {
                dmDichVuChiTietBusiness.DeleteDmDichVuChiTiet(dm.MaChiTieu);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadDataGrid(sender, e);
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}